﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using HRX.PayCenter.ApplicationCore.Models;
using Ardalis.GuardClauses;
using HRX.PayCenter.ApplicationCore.Exceptions;
using HRX.PayCenter.Common.WebAppBase;
using OSS.Common.Encrypt;
using OSS.Common.Extention;
using HRX.PayCenter.Common;
using HRX.PayCenter.ApplicationCore.Interfaces;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace HRX.PayCenter.Web.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [Route("api/[controller]")]
    public class CommonController : Controller
    {
        private readonly IAppLogger<CommonController> _appLogger;

        public CommonController(IAppLogger<CommonController> appLogger)
        {
            _appLogger = appLogger;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="seed"></param>
        /// <returns></returns>
        [HttpGet("GetSecurityKey")]
        public async Task<IActionResult> GetSecurityKey([FromQuery]string seed = "MIIBIjANBgkqhk")
        {
            var securityKey = OSS.Common.Encrypt.Md5.EncryptHexString(seed).ToUpper();
            return await Task.FromResult(Ok(securityKey));
        }

        /// <summary>
        /// 获取加密扩展字段
        /// </summary>
        /// <param name="securityKey"></param>
        /// <param name="extModel"></param>
        /// <returns></returns>
        [HttpPost("GetEncryptExt")]
        public async Task<IActionResult> GetEncryptExt([FromQuery] string securityKey,
            [FromBody] AlipayTradeExtModel extModel)
        {
            if (extModel == null)
            {
                Guard.Against.NullException();
            }

            string encrptyStr = NewtonsoftJsonHelper.SerializeObject(extModel);
            var result = AesRijndael.Encrypt(encrptyStr, securityKey).Base64UrlEncode();

            return await Task.FromResult(Ok(result));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="securityKey"></param>
        /// <param name="toDecrypt"></param>
        /// <returns></returns>
        [HttpGet("GetDecryptExt")]
        public async Task<IActionResult> GetDecryptExt(string securityKey, string toDecrypt)
        {
            if (string.IsNullOrEmpty(securityKey) || string.IsNullOrEmpty(toDecrypt))
            {
                Guard.Against.NullException();
            }

            toDecrypt = toDecrypt.Base64UrlDecode();

            var decryptStr = AesRijndael.Decrypt(toDecrypt, securityKey);
            var result = NewtonsoftJsonHelper.DeserializeObject<AlipayTradeExtModel>(decryptStr);

            return await Task.FromResult(Ok(result));
        }

        [HttpGet("WriteLog")]
        public async Task<IActionResult> WriteLog(string info, string logLevel)
        {
            if (logLevel == "error")
            {
                _appLogger.LogError(null, info);
            }
            else if (logLevel == "info")
            {
                _appLogger.LogInformation(info);
            }
            else
            {
                _appLogger.LogWarning(info);
            }
            return await Task.FromResult(Ok("成功"));
        }
    }
}
